#include "main.h"

//用于获取文件
void gets_file(int netFD, char* name){

    //创建文件
    int fd = open(name, O_RDWR|O_CREAT|O_TRUNC, 0600);
    ERROR_CHECK(fd, -1, "open_getsFile.c");
    //预留文件存储大小
    off_t file_size;
    int ret = recv(netFD, &file_size, sizeof(off_t), MSG_WAITALL);
    ERROR_CHECK(ret, -1, "recv_filesize_getsFile.c");
    ret = ftruncate(fd, file_size);
    //接收文件
    char *p = (char *)mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    ERROR_CHECK(p, NULL, "mmap_getsFile");
    ret = recv(netFD, p, file_size, MSG_WAITALL);
    ERROR_CHECK(ret, -1, "recv_file_getsFile.c");
    
    munmap(p, file_size);

    close(fd);
}
